﻿namespace ImageProcessing.UI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// IMG_GrayScaleScreen form
    /// </summary>
    public partial class IMG_GrayScaleScreen : Form
    {
        /// <summary>
        /// original image bitmap
        /// </summary>
        private Entities.IMG_Image imgOriginal;

        /// <summary>
        /// edited image bitmap
        /// </summary>
        private Entities.IMG_Image imgEdited;

        /// <summary>
        /// parent Form referance
        /// </summary>
        private UI.IMG_HomeScreen parentForm;

        /// <summary>
        /// Initializes a new instance of the IMG_GrayScaleScreen class.
        /// </summary>
        /// <param name="img_original">The img_original.</param>
        /// <param name="parent">The parent.</param>
        public IMG_GrayScaleScreen(Entities.IMG_Image img_original, UI.IMG_HomeScreen parent)
        {
            try
            {
                this.InitializeComponent();
                this.parentForm = parent;
                if (img_original != null)
                {
                    this.imgOriginal = img_original;
                    this.picBoxOrg.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgOriginal);
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Loading Form");
            }
        }

        /// <summary>
        /// Resets the image.
        /// </summary>
        /// <param name="image">The image.</param>
        public void ResetImage(Entities.IMG_Image image)
        {
            try
            {
                this.imgOriginal = image;
                this.imgEdited = null;
                this.picBoxEdit.Image = null;
                if (this.imgOriginal == null)
                {
                    return;
                }

                this.picBoxOrg.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgOriginal);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error loading image ");
            }
        }

        /// <summary>
        /// open button
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void Btn_Open_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.ppm)|*.jpg; *.jpeg; *.gif; *.bmp; *.ppm";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string img_OpenedFilePath = openFileDialog1.FileName;
                    this.imgOriginal = IMG_Processor.IMG_OpenSave.IMG_Open(img_OpenedFilePath);
                    this.picBoxOrg.Image = this.imgOriginal.BitmapOfImage();
                    this.imgEdited = null;
                    this.picBoxEdit.Image = null;
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, Can't Open Image");
            }
        }

        /// <summary>
        /// grey scale button
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event</param>
        private void Btn_GrayScale_Click(object sender, EventArgs e)
        {
            try
            {
                this.imgEdited = IMG_Processor.IMG_GrayScale.IMG_GrayScaleBuffer((this.imgEdited == null ? this.imgOriginal : this.imgEdited));
                this.picBoxEdit.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("ERROR!! Gray Scalling the Image");
            }
        }

        /// <summary>
        /// Handles the FormClosing event of the IMG_GrayScaleScreen control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void IMG_GrayScaleScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.parentForm.ResetImage(this.imgEdited == null ? this.imgOriginal : this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, closing");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_Slice control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_Slice_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.imgEdited == null)
                {
                    this.imgEdited = IMG_Processor.IMG_GrayScale.IMG_GrayScaleBuffer(this.imgOriginal);
                }

                UI.IMG_Slicing formSlicing = new IMG_Slicing(this.imgEdited, this);
                formSlicing.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("ERROR!! slicing the Image");
            }
        }

        /// <summary>
        /// Handles the Click event of the btn_GreyLevelSlicing control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_GreyLevelSlicing_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.imgEdited == null)
                {
                    this.imgEdited = IMG_Processor.IMG_GrayScale.IMG_GrayScaleBuffer(this.imgOriginal);
                }

                UI.IMG_GreyLevelSlicing formSlicing = new IMG_GreyLevelSlicing(this.imgEdited, this);
                formSlicing.Show();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("ERROR!! slicing the Image");
            }
        }

        /// <summary>
        /// Handles the Click event of the BtnBinary control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnBinary_Click(object sender, EventArgs e)
        {
            try
            {
                this.imgEdited = ImageProcessor.IMG_BinaryImage.ConvertToBinaryImage((this.imgEdited == null ? this.imgOriginal : this.imgEdited), int.Parse(this.txtThreshold.Text));
                this.picBoxEdit.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("ERROR!! Binarizing the Image");
            }
        }
    }
}
